package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiresPermissions("employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        //共享分页数据
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //共享所有部门数据

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        return "employee/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions("employee:delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
            employeeService.deleteMiddleTable(id);
        }
        return "redirect:/employee/list";  //返回的是上面查询所偶有所的方法
    }

    @RequestMapping("/input")
    @RequiresPermissions("employee:input")
    public String input(Long id, Model model) {
        //共享所有部门数据
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);

        //遍历角色信息
        List<Role> roleList = roleService.listAll();
        model.addAttribute("roleList", roleList);
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);

            //查找员工角色
            List<Role> employeeRoles = roleService.selEmployeeRoleById(id);
            model.addAttribute("employeeRoles", employeeRoles);
        }
        return "employee/input";

    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions("employee:saveOrUpdate")
    public JsonResult saveOrUpdate(Employee employee, Long[] roleIds) {
        System.out.println(employee);
      //  try {
            employeeService.save(employee, roleIds);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new JsonResult(false, e.getMessage());
//        }
        return new JsonResult(true, "保存成功");
    }

    //检查用户名是否存在
    @RequestMapping("/checkUsername")
    @ResponseBody
    @RequiresPermissions("employee:saveOrUpdate")
    public Map<String, Boolean> checkUsername(String username) {
        Map<String, Boolean> map = new HashMap<>();
        Boolean flag = employeeService.checkUsername(username);
        map.put("valid", flag);
        return map;

    }
}
